Completed
Push — master ( e0892a...db4e0d )
by
unknown
01:51
created

revision.js ➔ getFilesRevision   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 18
dl 0
loc 48
rs 8.551
cc 6
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
D revision.js ➔ ... ➔ urls.forEach 0 28 9
1
import {
2
  FileParser,
3
  fileUtils,
4
  config,
5
  cmsData,
6
  Manager
7
} from '../../'
8
9
export function getFilesRevision(urls, fileName) {
10
  var res = []
11
  var number = 1
12
  var tplUrl = FileParser.getFileDataFromUrl(fileName)
13
  fileName = fileName.split('/')
14
  fileName = fileName[fileName.length - 1]
15
  var publishDate = new Date()
16
  var json = null
17
18
  if(fileUtils.isFile(tplUrl.publish.json)) {
19
    json = FileParser.getJson(tplUrl.publish.json)
20
    if(typeof json !== 'undefined' && json !== null
21
      && typeof json[config.meta.name] !== 'undefined' && json[config.meta.name] !== null) {
22
      publishDate = new Date(json[config.meta.name].latest.date)
23
    }
24
  }
25
26
  var publishVersion = false
27
  urls.forEach(function (urlObj) {
28
    var fileData = cmsData.fileAttr.get(urlObj.cleanPath)
29
    if(fileData.s === 'd' && cmsData.fileAttr.delete(urlObj.cleanPath) == cmsData.fileAttr.delete(fileName)) {
30
      var currentDate = new Date(urlObj.date)
31
      if(currentDate.getTime() > publishDate.getTime()) {
32
        if(!publishVersion && typeof res[res.length - 1] !== 'undefined' && res[res.length - 1] !== null) {
33
          res[res.length - 1].publishedDate = 'same'
34
        }
35
        publishVersion = true
36
        urlObj.publishedDate = 'after'
37
        
38
      }else if(currentDate.getTime() === publishDate.getTime()) {
39
        urlObj.publishedDate = 'same'
40
        publishVersion = true
41
      }else {
42
        urlObj.publishedDate = 'before'
43
      }
44
      urlObj.version = number
45
      number = number + 1
46
47
      var tplUrlObj = FileParser.getFileDataFromUrl(urlObj.path)
48
      if(fileUtils.isFile(tplUrlObj.publish.json)) {
49
        var jsonObj = FileParser.getJson(tplUrlObj.publish.json)
50
        urlObj[config.meta.name] = jsonObj[config.meta.name]
51
      }
52
      res.push(urlObj)
53
    }
54
  })
55
  return res
56
}
57
58
/**
59
 * Create and array of doc file path containing the versions of this doc
60
 * @param  {String} path of a doc
0 ignored issues
show
Documentation introduced by
The parameter path does not exist. Did you maybe forget to remove this comment?
Loading history...
61
 * @return {Array} the versions of the doc
62
 */
63
export function getVersions(docPath) {
64
  var result = []
65
  var files = Manager.instance.getList()
66
  var dataFile = docPath.replace('.' + config.files.templates.extension, '.json')
67
68
  Array.prototype.forEach.call(files, (file) => {
69
    if (file.path.indexOf(dataFile) > -1) {
70
      result = file.revisions
71
    }
72
  })
73
  return result
74
}
75
76
/**
77
 * Return the revision from document html file path
78
 * if the docPath contains abe revision [status]-[date] will try to return this revision
79
 * else it will return the latest revision
80
 * or null
81
 * 
82
 * @param  {String} html path
0 ignored issues
show
Documentation introduced by
The parameter html does not exist. Did you maybe forget to remove this comment?
Loading history...
83
 * @return {Object} file revision | null
84
 */
85
export function getDocumentRevision(docPath) {
86
  var result = null
87
  var documentPath = docPath
88
  var latest = true
89
90
  if(cmsData.fileAttr.test(documentPath)){
91
    latest = false
92
    documentPath = cmsData.fileAttr.delete(documentPath)
93
  }
94
  var revisions = getVersions(documentPath)
95
  if (latest && revisions.length >= 0) {
96
    result = revisions[0]
97
  }else if (!latest) {
98
    Array.prototype.forEach.call(revisions, (revision) => {
99
      if (revision.html === docPath) {
100
        result = revision
101
      }
102
    })
103
    if (result === null && revisions.length >= 0) {
104
      result = revisions[0]
105
    }
106
  }
107
  return result
108
}
109
110
export function getStatusAndDateToFileName(date) {
111
  var res = date.substring(0, 4) + '-'
112
            + date.substring(4, 6) + '-'
113
            + date.substring(6, 11) + ':'
114
            + date.substring(11, 13) + ':'
115
            + date.substring(13, 15) + '.'
116
            + date.substring(15, 19)
117
  return res
118
}
119
120
export function removeStatusAndDateFromFileName(date) {
121
  return date.replace(/[-:\.]/g, '')
122
}
123
124
export function filePathInfos(pathFolder) {
125
  var pathArr = pathFolder.split('/')
126
  var name = pathArr[pathArr.length - 1]
127
128
  var rootArr = config.root.split('/')
129
  var website = rootArr[pathArr.length - 1]
130
  return {
131
    'name': name,
132
    'path': pathFolder,
133
    'website': website,
134
    'cleanPath': fileUtils.cleanPath(pathFolder.replace(config.root, '')),
135
    'type': 'folder'
136
  }
137
}